import http from '../dxmodules/dxHttp.js'
import log from '../dxmodules/dxLogger.js'
import * as os from "os";


// Set request headers
let requestHeaders = []
requestHeaders[0] = "Accept-Charset: utf-8"
requestHeaders[1] = "Content-Type: application/json;charset=utf-8"
requestHeaders[2] = "Connection: keep-alive"

//GET request
let res
try {
    res = http.get("tools.dxiot.com/dxdop/webadmin/componentModel/findAll?page=1&size=10&modelName=mqtt", requestHeaders)
} catch (error) {
    log.error('Request failed: ', error)
    return
}
let response = JSON.parse(res);
// GET request returned content
log.info("GET request returned content: ", response.body)

os.sleep(3000)
//POST request
let res1
try {
    res1 = http.post("http://101.200.139.97:8391/webadmin/tools/vg/passCode/getQrcode/v103", JSON.stringify({ code: '1234', expireTime: '123', orgCode: '123' }), requestHeaders)
} catch (error) {
    log.error('Request failed: ', error)
    return
}

let response1 = JSON.parse(res1);
log.info("POST request returned content: ", response1.body)



//Download file URL and file save path plus file extension
let downloadRes = http.download('http://101.200.139.97:8391/webadmin/firmware/downloadFile/APP_3_8.zip', '/app.zip')
log.info(downloadRes);